﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAAC.DTO;
using System.Data;

namespace SAAC.DAL
{
	public class ComissaoDAL : DALBase
	{

		public ComissaoDTO Select(ComissaoDTO dto, string order = null)
		{
			try
			{
				string sql = "CALL sp_comissao_sel(@id,@formacao,@fim,@order,@limit);";

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@formacao", dto.Formacao);
				cmd.Parameters.AddWithValue("@fim", dto.Fim);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", 1);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				if (dr.Read())
				{
					dto.Id = Convert.ToInt32(dr["idComissao"]);
					dto.Formacao = Convert.ToDateTime(dr["formacao"]);
					dto.Fim = Convert.ToDateTime(dr["fim"]);
				}
				else dto = null;

				return dto;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public List<ComissaoDTO> SelectAll(ComissaoDTO dto, string order = null, int limit = 1000)
		{
			try
			{
				string sql = "CALL sp_comissao_sel(@id,@formacao,@fim,@order,@limit);";

				var lista = new List<ComissaoDTO>();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@formacao", dto.Formacao);
				cmd.Parameters.AddWithValue("@fim", dto.Fim);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", limit);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				while (dr.Read())
				{
					var item = new ComissaoDTO();
					item.Id = Convert.ToInt32(dr["idComissao"]);
					item.Formacao = Convert.ToDateTime(dr["formacao"]);
					item.Fim = Convert.ToDateTime(dr["fim"]);
					lista.Add(item);
				}

				return lista;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Insert(ComissaoDTO dto)
		{
			try
			{
				String sql = @"CALL sp_comissao_ins(@formacao,@fim);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@formacao", dto.Formacao);
				cmd.Parameters.AddWithValue("@fim", dto.Fim);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Update(ComissaoDTO dto)
		{
			try
			{
				String sql = "CALL sp_comissao_upd(@id,@formacao,@fim);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@formacao", dto.Formacao);
				cmd.Parameters.AddWithValue("@fim", dto.Fim);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Delete(ComissaoDTO dto)
		{
			try
			{
				String sql = "CALL sp_comissao_del(@id);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}
	}
}
